package com.liu.controller;

import com.liu.util.HttpServletFactory;
import com.liu.util.LoggerFactory;
import com.liu.util.Page;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author 老刘Matthew
 * @Timestamp 2022/7/13 12:05
 * @Description
 */
@WebServlet("/")
public class FrontServlet extends GenericServlet implements HttpServletFactory, LoggerFactory {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) {
        this.doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) {

        try {

            String path = request.getContextPath();

            logger.info("----------> contextPath : " + path);

            String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";

            logger.info("----------> basePath : " + basePath);

            // 页面使用 BASE_PATH 常量
            request.getSession().setAttribute("BASE_PATH", basePath);

            new GenericServletBuilder(request, response, this.getClass()) //
                    .setUrl(Page.INDEX.get())
                    .build()
                    .getRequestMapping();

        } catch (Exception e) {
            logger.error("控制转发异常！", e);
        }

    }
}
